package com.tencent.trtc.apiexample.ui.dashboard;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.alibaba.fastjson.*;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.tencent.trtc.apiexample.CreateRoomActivity;
import com.tencent.trtc.apiexample.MainActivity;
import com.tencent.trtc.apiexample.R;
import com.tencent.trtc.apiexample.RoomInfo;
import com.tencent.trtc.apiexample.WebServiceGet;
import com.tencent.trtc.apiexample.mAdapter;
import com.tencent.trtc.videocall.VideoCallingEnterActivity;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import kotlin.random.URandomKt;

import static android.media.CamcorderProfile.get;
import static com.tencent.trtc.apiexample.LoginActivity.PATH;

public class DashboardFragment extends Fragment {

    private String path = "";
    private String result = "";
    private ListView mRoomListView;
    private ArrayList<RoomInfo> room;

    private ArrayList<RoomInfo> RoomInfos; //声明一个List的全局变量 roomInfo

    //private DashboardViewModel dashboardViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_dashboard , container, false);
        mRoomListView = (ListView) view.findViewById(R.id.list_view);
        room = getData();
        while(room.size()==0);
        mRoomListView.setAdapter(new mAdapter(getActivity(), room));
        return view;
    }

    public ArrayList<RoomInfo> getData(){
        ArrayList<RoomInfo> list = new ArrayList<RoomInfo>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                path = PATH + "/roomselect";
                result = WebServiceGet.executeHttpGet(path);
                room = (ArrayList<RoomInfo>) JSONObject.parseArray(result,RoomInfo.class);
            }
        }).start();
        return list;
    }

    @Override
    public void onActivityCreated(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getView().findViewById(R.id.btn_create_room).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), CreateRoomActivity.class);
                startActivity(intent);
            }
        });


        //设置点击事件
        mRoomListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LinearLayout linearLayout = (LinearLayout)view;
                TextView tvRoomName = linearLayout.findViewById(R.id.tv_1);
                TextView tvRoomID = linearLayout.findViewById(R.id.tv_2);
                TextView tvRoomMaster = linearLayout.findViewById(R.id.tv_3);
                String roomName = tvRoomName.getText().toString();
                String roomID = tvRoomID.getText().toString();
                String roomMaster = tvRoomMaster.getText().toString();

                Intent intent = new Intent(getActivity(),VideoCallingEnterActivity.class);
                intent.putExtra("Enter_RoomName",roomName);
                intent.putExtra("Enter_RoomID",roomID);
                startActivity(intent);
            }
        });
    }
}